package cn.csust.net2.manager.client.panel;

import java.util.ArrayList;
import java.util.List;

import cn.csust.net2.manager.client.Interaction.MailWriteWindow;
import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.ux.BeanGrid;
import cn.csust.net2.manager.client.ux.BeanGridPanel;
import cn.csust.net2.manager.shared.po.InnerMail;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.service.InnerMailService;
import cn.csust.net2.manager.shared.service.InnerMailServiceAsync;
import cn.csust.net2.manager.shared.vo.Config;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
/**
 * 
 * 站内信
 * @author Administrator
 *
 */
public class InnerMailPanel extends ContentPanel{
	
	
	private InnerMailServiceAsync mailService;
	
	private ContentPanel contentPanel;
	private BeanGridPanel<InnerMail> mailGridPanel;
	private FormPanel mailCheckPanel;
	
	private BeanGrid<InnerMail> grid;
	
	//右边form和左边的grid绑定
	private FormBinding formBindings;
	
	
	TextField<String> titleFiled;
	TextField<String> senderFiled;
	TextField<String> receiverField;
	HtmlEditor content ;

	private ToolBar toolBar;
	
	public InnerMailPanel() {

		this.setHeaderVisible(false);
		this.setLayout(new FitLayout());
		
		init();

	}
	public void init(){
		this.setIcon(AbstractImagePrototype.create(Resources.ICONS.writeMail()));
		mailService =  GWT.create(InnerMailService.class);
		
		createContentPanel();

	}
	
	
	private void createContentPanel(){
		contentPanel = new ContentPanel();
		
		contentPanel.setHeaderVisible(false);
		contentPanel.setLayout(new BorderLayout());   
		
		createToolBar();
		contentPanel.setTopComponent(toolBar);
		
		createMailGridPanel();
		createMailCheckPanel();
		this.add(contentPanel);
	}
	
	//站内信表格
	private void createMailGridPanel(){

		mailGridPanel = new BeanGridPanel<InnerMail>(new InnerMail(),mailService);
		mailGridPanel.createGrid();
		mailGridPanel.createPagingToolBar();
		Config loadConfig = new Config();
		loadConfig.put("user", App.getUser());
		mailGridPanel.load(loadConfig);

		grid = mailGridPanel.getGrid();
		
		//当一行数据被选择的时候在右边显示
		mailGridPanel.getGrid().getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {
			public void handleEvent(
					SelectionChangedEvent<BeanModel> be) {
				if (be.getSelection().size() == 1) {
					formBindings.bind((ModelData) be
							.getSelection().get(0));
				} else {
					formBindings.unbind();
				}
			}
		});

		
		
		FieldSet fieldSet = new FieldSet();   
		fieldSet.setHeading("收件箱");
		fieldSet.setLayout(new FitLayout());
		fieldSet.add(mailGridPanel);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(10,10,10,10));
		contentPanel.add(fieldSet, centerData); 
		
	}
	//与数据绑定的form
	private void createMailCheckPanel(){

		mailCheckPanel = new FormPanel();   
		mailCheckPanel.setBorders(false);   
		mailCheckPanel.setBodyBorder(false);   
		mailCheckPanel.setLabelWidth(55);   
		mailCheckPanel.setPadding(5);  
		
		titleFiled = new TextField<String>(); 
		titleFiled.setReadOnly(true);
	    titleFiled.setName("title");   
	    titleFiled.setFieldLabel("主题");   
	    mailCheckPanel.add(titleFiled); 
	   
	    senderFiled = new TextField<String>(); 
	    senderFiled.setName("sender");  
	    senderFiled.setReadOnly(true);
	    senderFiled.setFieldLabel("发送人");   
	    mailCheckPanel.add(senderFiled); 
	  
	    content = new HtmlEditor(); 
	    content.setShowToolbar(false);
	    content.setHideLabel(true);
	    content.setName("innerMailContent");
	    content.setReadOnly(true);   
	    mailCheckPanel.add(content, new FormData("100% -53")); 
		mailCheckPanel.setHeaderVisible(false); 
		formBindings = new FormBinding(mailCheckPanel, true);  
		
	    Button reply = new Button("回复");
	    reply.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				BeanModel beanModel = mailGridPanel.getGrid().getSelectionModel().getSelectedItem();
				InnerMail innerMail = beanModel.getBean();
				if(innerMail!=null){
					MailWriteWindow mww = new MailWriteWindow();
					mww.setReceiver(innerMail.getSender());
					mww.show();

				}
				
			}
		});
	    Button delete = new Button("删除");
	    delete.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				delete();
			}
			
		});
	    mailCheckPanel.addButton(reply);
	    mailCheckPanel.addButton(delete);
	    
	    FieldSet fieldSet = new FieldSet();   
	    fieldSet.setHeading("信件");
	    fieldSet.setLayout(new FitLayout());
	    fieldSet.add(mailCheckPanel);
		BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST,450);
		eastData.setMargins(new Margins(10,10,10,10));
		contentPanel.add(fieldSet, eastData);  
	    
	}

	
	private void createToolBar(){
		toolBar = new ToolBar();
		Button add = new Button("写信", AbstractImagePrototype.create(Resources.ICONS.mail()));
		add.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				MailWriteWindow mww = new MailWriteWindow();
				//在写站内信的地方添加用户树
				mww.recreateWindow();
				mww.show();
			}
		});
		Button delete = new Button("删除", AbstractImagePrototype.create(Resources.ICONS.toolBarDelete()));
		delete.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				delete();
			}
			
		});
		
		Button enableAllButton = new Button("全选", AbstractImagePrototype.create(Resources.ICONS.toolBarSelectAll()));
		enableAllButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				mailGridPanel.getGrid().getSelectionModel().selectAll();
			}
		});
		
		Button disableAllButton = new Button("全不选", AbstractImagePrototype.create(Resources.ICONS.toolBarSelectNo()));
		disableAllButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				mailGridPanel.getGrid().getSelectionModel().deselectAll();
			}
		});

		toolBar.add(add);
		toolBar.add(new SeparatorToolItem());
		toolBar.add(delete);
		toolBar.add(new SeparatorToolItem());
		toolBar.add(enableAllButton);
		toolBar.add(new SeparatorToolItem());
		toolBar.add(disableAllButton);

	}

	public void delete() {
		if (grid.getSelectionModel().getSelectedItems().size() == 0) {
			return;
		} else {
			MessageBox.confirm("警告", "您真的真的确定要删除所选信件么（删除后无法恢复）?",
					new Listener<MessageBoxEvent>() {

						@Override
						public void handleEvent(MessageBoxEvent be) {
							// TODO Auto-generated method stub
							Button btn = be.getButtonClicked();
							if (btn.getItemId().equals("yes")) {
								List<PO> list = new ArrayList<PO>();
								for (BeanModel model : grid.getSelectionModel().getSelectedItems()) {
									InnerMail t = model.getBean();
									list.add(t);
								}
								mailService.delete(list, new AsyncCallback<Void>() {

											@Override
											public void onFailure(Throwable caught) {
												// TODO Auto-generated
												// method stub
												Info.display("", "失败");
											}

											@Override
											public void onSuccess(Void result) {
												// TODO Auto-generated
												// method stub
												Config loadConfig = new Config();
												loadConfig.put("student", App.getUser());
												mailGridPanel.load(loadConfig);
												grid.getView().refresh(true);
											}

										});

								

							} else {
								return;
							}
						}
					});
		}
	}

}
